﻿$(function() {
    detail($("#is_orders_code").val());//填充<{$lang.order}>资料
    $("#comment-dialog").dialog({
        autoOpen:false,
        modal:false,
        show:"slide",
        resizable: false,
        width:600,
        close:function() {
            $("#commentForm")[0].reset();//重设表单<{$lang.data}>
            $('#commentTips').removeClass('ui-state-highlight').hide();
        }
    });
    $('#dialog-product').dialog({autoOpen:false,width: 700,modal: false,resizable: false,show:"slide"});
});

function switchTab(id) {
    $('.orderError', '#tabs').text('').hide();
    var orders_code = $("#is_orders_code").val();
    if (id == 1) {

    } else if (id == 3) {
        $('#log_order_id').text(orders_code);
        initData(0);
    }
    for (var i = 1; i <= 3; i++) {
        if (i == id) {
            $("#tabs-li-" + i).addClass('tabs-head-active');
            $("#tabs-" + i).show();
        } else {
            $("#tabs-li-" + i).removeClass('tabs-head-active');
            $("#tabs-" + i).hide();
        }
    }
}

function loadData(page, pageSize) {
    loading();
    $("#historyData").empty();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/order/order/statushistory/page/" + page + "/pageSize/" + pageSize,
        data:"orders_code=" + $("#is_orders_code").val(),
        success: function(json) {
            closeLoading();
            var historyHtml = "";
            paginationTotal = json.count;
            if (json.ask == 0) {
                historyHtml = "<div align='center'>&nbsp;<{$lang.no_data}></div>";
            } else {
                $.each(json.result, function(key, info) {
                    var rowStyle = (key % 2 == 0) ? "divGray" : "divWhite";
                    info.name = info.name ? info.name : '<{$lang.system}>';
                    historyHtml += "<div class=\"" + rowStyle + "\">";
                    historyHtml += "<div class=\"historyCol6\" title=\"" + info.name + "\">" + info.name + "</div>";
                    historyHtml += "<div class=\"historyCol4\" title=\"" + info.osh_create_date + "\">" + info.osh_create_date + "</div>";
                    historyHtml += "<div class=\"historyCol2\" title=\"" + info.osh_type + "\">" + info.osh_type + "</div>";
                    var comment = '';
                    comment = info.osh_comments;
                    historyHtml += "<div class=\"historyCol5\" title=\"" + comment + "\">" + comment + "</div>";
                    historyHtml += "<div class=\"historyCol1\" title=\"" + info.osh_remark + "\">&nbsp;" + info.osh_remark + "</div>";
                    historyHtml += "<div class=\"myClear\"></div>";
                    historyHtml += "</div>";
                });
                historyHtml += "<div style=\"display:none;\" id=\"orders_id\">" + json.order_code + "</div>";
            }
            $("#historyData").html(historyHtml);
        }
    });
}

//<{$lang.create}><{$lang.log}>
function addComments() {
    $('#comment-dialog').dialog('option', 'buttons', {
        '<{$lang.ok}>': function() {
            var oDialog = $(this);
            var vForm = 'orders_code=' + $('#orders_code', '#baseForm').text() + '&' + $('#commentForm').serialize();
            $.post("/order/order/addcomment", vForm, function(data) {
                if (data == null) return;
                else if (data.ask == 1) {
                    initData(0);
                    oDialog.dialog('close');
                } else {
                    $('#commentTips').text(data.msg).show();
                }
            }, 'json');
        },
        '<{$lang.cancel}>': function() {
            $("#commentForm")[0].reset();
            $(this).dialog('close');
        }
    }).dialog("open");
}

//查看<{$lang.detail}>
function detail(orders_code) {
    httpRequest = $.ajax({url:"/order/order/info", type:"GET", data:"orders_code=" + orders_code, dataType:"json", success:detailResponse});
}
function detailResponse(json) {
    if (json == null) return;
    if (json.ask == 1) {
        var os = json.result;
        //<{$lang.order}><{$lang.msg}>
        $(".bill-item-right", "#tabs-1 #baseForm").each(function() {
            var order_id = this.id;
            if (os[order_id] != null) {
                $(this).html('<span>' + os[order_id] + '</span>');
            }
        });
        //收件人<{$lang.msg}>
        $("[name=orders_id]").val(os.order_id);
        $("[name=orders_code]").val(os.orders_code);
        $(".bill-item-right", "#tabs-2").each(function() {
            var consignee_id = this.id;
            if (os[consignee_id] != null) {
                $(this).html('<span>' + os[consignee_id] + '</span>');
            }
        });
        //<{$lang.product}><{$lang.list}>
        var productHtml = '';
        $.each(json.productList, function(key, info) {
            var rowStyle = (key % 2 == 0) ? "divGray" : "divWhite";
            var product_img = (info.product_img != '' && info.product_img != undefined) ? '&nbsp;<img src=' + info.product_img + ' />' : '&nbsp;';
            s_product_id = info.product_id;
//            s_product_id = '<a href="javascript:proDetails(' + info.op_id + ')">' + info.product_id + '</a>';
            s_product_id = '' + info.product_id + '';
            productHtml += "<div id=\"op" + info.op_id + "\" class=\"" + rowStyle + "\" style=\"height:75px;\">";
			
//			$.each(info.proIamges,function(ik,iv){
//				productHtml += '<div class="columnImg product-img-view" '+(ik!=0 ? 'style="display:none;"' : '')+'><a class="fancybox-thumbs" data-fancybox-group="thumb" href="/product/product/view-qc-picture/paid/'+iv+'"><img style="width:75px;height:75px;" src="/product/product/view-qc-picture/paid/'+iv+'"></a></div>';		
//			});
			productHtml += '<div class="columnImg product-img-view" ><img style="width:75px;height:75px;" src="/product/product/view-picture/id/'+info.product_id+'"></div>';		

            productHtml += "<div class=\"column2\">" + s_product_id + "</div>";
            productHtml += "<div class=\"column2_1\">" + os.customer_id+"-"+info.product_sku + "</div>";
            productHtml += "<div class=\"column3\">" + info.product_title + "</div>";
            productHtml += "<div class=\"column4\"><span>" + parseInt((info.op_subtotal/info.op_quantity)*100)/100 + "</span></div>";
            productHtml += "<div class=\"column5\"><span>" + info.op_quantity + "</span></div>";
            productHtml += "<div class=\"column6\">" + info.subtotal + "</div>";
//            productHtml += "<div class=\"column8\"><a href=\"javascript:delProduct(" + info.op_id + ");\"><{$lang.delete}></a></div>";
            productHtml += "<div class=\"column8\"></div>";
             productHtml += "</div>";
        });
        //统计
        $("#subtotal", ".bill-product-table").text(json.subtotal);
        $("#productData").html(productHtml);
    }
}

//<{$lang.delete}><{$lang.product}>
function delProduct(op_id) {
    if ($('#productData').children().length <= 1) {
        alert('<{$lang.at_least_one}><{$lang.product}>');
        return;
    }
    $("#comment-dialog").dialog('option', 'buttons', {
        '<{$lang.ok}>': function() {
            var sPost = "orders_code=" + $("#is_orders_code").val() + "&op_id=" + op_id;
            sPost += '&' + $('#commentForm').serialize();
            ;
            $.post("/order/order/deleteproduct", sPost, function(data) {
                if (data == null) return;
                if (data.ask == '1') {
                    $("#comment-dialog").dialog('close');
                    $("#op" + data.op_id).remove();
                    $("#subtotal", ".bill-product-table").text(data.order_subtotal);
                    $("#parcel_value", "#tabs-1 #baseForm").text(data.order_subtotal);
                } else if (data.ask == '0') {
                    alertTip(data.msg);//<{$lang.fail}>
                }
            }, 'json');
        },
        '<{$lang.cancel}>': function() {
            $(this).dialog('close');
        }
    }).dialog("open");
}

//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}

var currentForm = '';
//<{$lang.edit}><{$lang.order}>
function updateOrder(formID) {
    currentForm = formID;
    $(".bill-item-right", "#" + formID).each(function() {
        if (formID == 'shipForm' && ( this.id == 'orders_id' )) {
            return;
        }
        if (formID == 'shipForm' && ( this.id == 'consignee_country_id' )) {

            return;
        }
        var jDiv = $(this);
        jDiv.children().first().hide();
        var style = 'style="width:153px;"';
        if (this.id.indexOf('address') != -1) {
            style = 'style="width:600px;"';
        }
        jDiv.append('<input type="text" name="' + this.id + '" value="' + jDiv.text() + '" ' + style + ' />');
    });
    var confirmLink = '<a href="javascript:confirmOrder(\'' + formID + '\');"><{$lang.ok}></a>';
    var cancelLink = '<a href="javascript:cancelOrder(\'' + formID + '\');"><{$lang.cancel}></a>';
    $(".orderDetailHeadLink", "#" + formID).html(confirmLink + "&nbsp;&nbsp;&nbsp;" + cancelLink);
}
function confirmOrder(formID) {
    currentForm = formID;
    $.post('/order/order/modify', $("#" + formID).serialize() + '&formID=' + formID, confirmCallback, 'json');
}
function confirmCallback(data) {
    if (data == null) return;
    if (data.ask == '0') {
        $('.orderError', '#tabs').html('<p>' + data.msg.toString() + '</p>').show();
    } else {
        $(".bill-item-right", "#" + data.formID).each(function() {
            if (this.id == 'orders_code' || this.id == 'orders_id') {
                return;
            }
            var jDiv = $(this);
            jDiv.children().first().text(jDiv.children().last().val()).show();
            jDiv.children().last().remove();
        });
        $(".orderDetailHeadLink", "#" + data.formID).html('<a href="javascript:updateOrder(\'' + data.formID + '\');"><{$lang.edit}></a>');
        $('.orderError', '#tabs').hide();
    }
}
function cancelOrder(formID) {
    $(".bill-item-right", "#" + formID).each(function() {
        if (this.id == 'orders_id') {
            return;
        }
        var jDiv = $(this);
        jDiv.children().last().remove();
        jDiv.children().first().show();
    });
    $(".orderDetailHeadLink", "#" + formID).html('<a href=\'javascript:updateOrder("' + formID + '");\'><{$lang.edit}></a>');
    $('.orderError', '#tabs').hide();
}
function addProduct(orders_code) {
    if (orders_code == '') {
        return false;
    }
    var url = '/order/order/product/orders_code/' + orders_code;
    window.parent.$('<iframe id="ifr_order_product_list" name="ifr_order_product_list" src="' + url + '" />').dialog({
        title: '<{$lang.pls}><{$lang.product}>',
        autoOpen: true,
        width: 760,
        modal: false,
        resizable: false,
        position:'top',
        autoResize: true,
        close:function() {
            $(this).remove();
        },
        buttons: {
            'Close/<{$lang.close}>': function() {
                $(this).remove();
            }
        }
    }).width(730).height(376);
}